package com.jzx.provider.cv.service;

import com.jzx.provider.common.utils.BeanUtil;
import com.jzx.provider.common.utils.DateUtil;
import com.jzx.provider.cv.dto.command.WorkInfoCommand;
import com.jzx.provider.cv.model.WorkInfo;
import com.jzx.provider.cv.repository.CvRepository;
import com.jzx.provider.cv.repository.WorkInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author daihuihui
 * @date 2019/2/5
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WorkInfoService {
    @Autowired
    private WorkInfoRepository workInfoRepository;

    @Autowired
    private CvRepository cvRepository;

    public void saveOrUpdate(WorkInfoCommand workInfoCommand) {
        WorkInfo workInfo = WorkInfo.builder().build();
        BeanCopier.create(WorkInfoCommand.class, WorkInfo.class, false)
                .copy(workInfoCommand, workInfo, null);
        workInfoRepository.save(workInfo);
        //更新简历时间
        cvRepository.findById(workInfoCommand.getCvId()).ifPresent(cv -> {
            cvRepository.save(cv);
            //更新es索引
        });
    }

    public void deleteWork(Integer id) {
        workInfoRepository.findById(id).ifPresent(workInfo -> {
            workInfoRepository.deleteById(id);
            //更新简历时间
            cvRepository.findById(workInfo.getCvId()).ifPresent(cv -> {
                cv.setUpdateTime(new Date());
                cvRepository.save(cv);
                //更新es索引
            });
        });
    }

    public void saveOrUpdateWorkInfo(WorkInfoCommand workInfoCommand) {
        //处理开始和结束日期
        if (!"至今".equals(workInfoCommand.getStartDate())) {
            workInfoCommand.setStartDate(new SimpleDateFormat("yyyy-MM").format(DateUtil.strToDate(workInfoCommand.getStartDate().replace("Z", " UTC"), "yyyy-MM-dd'T'HH:mm:ss.SSS Z")));
        }
        if (!"至今".equals(workInfoCommand.getEndDate())) {
            workInfoCommand.setEndDate(new SimpleDateFormat("yyyy-MM").format(DateUtil.strToDate(workInfoCommand.getEndDate().replace("Z", " UTC"), "yyyy-MM-dd'T'HH:mm:ss.SSS Z")));
        }
        WorkInfo workInfo = WorkInfo.builder().build();
        if (null != workInfoCommand.getId()) {
            BeanCopier.create(WorkInfoCommand.class, WorkInfo.class, false).copy(workInfoCommand, workInfo, null);
            workInfo.setVocation(String.join("|", workInfoCommand.getVocation()));
            workInfo.setUpdateTime(new Date());
            workInfoRepository.updateCvWorkInfo(workInfo);
        } else {
            BeanCopier.create(WorkInfoCommand.class, WorkInfo.class, false).copy(workInfoCommand, workInfo, null);
            workInfo.setVocation(String.join("|", workInfoCommand.getVocation()));
            workInfo.setCreateTime(new Date());
            workInfo.setUpdateTime(new Date());
            workInfoRepository.save(workInfo);
        }
        cvRepository.findById(workInfo.getCvId()).ifPresent(cv -> {
            cv.setUpdateTime(new Date());
            cv.setIsIndex(0);
            cvRepository.save(cv);
        });
    }
}
